package bb.lanxing.lib.devices.bryton.ncs;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Pair;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import bb.lanxing.lib.devices.bryton.bbcp.BbcpNotification;
import bb.lanxing.lib.devices.bryton.bbcp.BbcpValidationException;
import bb.lanxing.lib.devices.bryton.bbcp.BleManager;
import java.util.List;
import java.util.UUID;

public class BncsManager extends BleManager {
    private static final String BNCS_BROADCAST_CONNECTION = "com.brytonsport.bbcp.BNCS_BROADCAST_CONNECTION";
    public static final UUID BNCS_DATA_SERVICE_UUID = new UUID(4993778844497479053L, -7259066960687485054L);
    private static final UUID BNCS_PACKET_UUID = new UUID(4993704223735678349L, -7259066960687485054L);
    private static final String EXTRA_DATA = "status";
    protected OnStateChangeListener mCallback;
    private ExtensionData mExtensionData;
    private int mIndex;
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i) {
        }

        @Override
        public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i, int i2) {
            BncsManager.this._handleConnectionStateChange(bluetoothGatt, i, i2);
            if (BncsManager.this.mConnectionState == 0) {
                BncsManager.this.sendBroadcast(i);
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i) {
            if (i == 0) {
                BncsManager.this.logi("Services discovered");
                BncsManager.this.mConnectionState = -3;
                BncsManager.this.sendBroadcast(i);
                return;
            }
            BncsManager bncsManager = BncsManager.this;
            bncsManager.loge("Service discovery error: " + i);
            BncsManager.this.mErrorState = i | 1024;
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
            if (i == 0) {
                BncsManager.access$708(BncsManager.this);
                BncsManager.this.writeAttribute(bluetoothGatt, bluetoothGattCharacteristic);
            }
        }
    };
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (!BncsManager.BNCS_BROADCAST_CONNECTION.equals(intent.getAction()) || BncsManager.this.mCallback == null) {
                return;
            }
            BncsManager.this.mCallback.onStateChanged(BncsManager.this.mConnectionState);
        }
    };

    public interface OnStateChangeListener {
        void onStateChanged(int i);
    }

    static /* synthetic */ int access$708(BncsManager bncsManager) {
        int i = bncsManager.mIndex;
        bncsManager.mIndex = i + 1;
        return i;
    }

    public BncsManager() {
        this.TAG = "BncsManager";
    }

    public void sendNotification(ExtensionData extensionData) {
        if (this.mConnectionState != -3) {
            loge("not connected and ready");
            return;
        }
        BluetoothGattCharacteristic servicePoint = getServicePoint();
        synchronized (this) {
            this.mExtensionData = extensionData;
            this.mIndex = 0;
        }
        writeAttribute(this.mBluetoothGatt, servicePoint);
    }

    public void writeAttribute(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        String str = "";
        String str2 = "";
        synchronized (this) {
            List<Pair<String, String>> attributes = this.mExtensionData.attributes();
            if (this.mIndex < attributes.size()) {
                str2 = (String) attributes.get(this.mIndex).first;
                str = (String) attributes.get(this.mIndex).second;
            }
        }
        if (str2.equals("") && str.equals("")) {
            this.mExtensionData = null;
            this.mIndex = 0;
            return;
        }
        try {
            byte[] pack = new BbcpNotification(this.mExtensionData.categoryID().value(), str2, str).pack();
            bluetoothGattCharacteristic.setValue(pack);
            logi("write package: " + toHexString(pack));
            bluetoothGatt.writeCharacteristic(bluetoothGattCharacteristic);
        } catch (BbcpValidationException e) {
            e.printStackTrace();
        }
    }

    BluetoothGattCharacteristic getServicePoint() {
        BluetoothGattService service = this.mBluetoothGatt.getService(BNCS_DATA_SERVICE_UUID);
        if (service == null) {
            return null;
        }
        return service.getCharacteristic(BNCS_PACKET_UUID);
    }

    public int connect(Context context, String str, OnStateChangeListener onStateChangeListener) {
        logi("connecting " + str);
        this.mErrorState = 0;
        this.mAborted = false;
        this.mConnectionState = -1;
        this.mContext = context;
        this.mCallback = onStateChangeListener;
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        if (bluetoothManager == null) {
            loge("Unable to initialize BluetoothManager.");
            return 272;
        }
        BluetoothAdapter adapter = bluetoothManager.getAdapter();
        if (adapter == null) {
            loge("Unable to obtain a BluetoothAdapter.");
            return 272;
        }
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this.mContext);
        localBroadcastManager.unregisterReceiver(this.mReceiver);
        localBroadcastManager.registerReceiver(this.mReceiver, makeIntentFilter());
        this.mBluetoothGatt = adapter.getRemoteDevice(str).connectGatt(context, true, this.mGattCallback);
        if (this.mBluetoothGatt == null) {
            loge("return null gatt");
            this.mErrorState = 272;
        }
        return this.mErrorState;
    }

    private IntentFilter makeIntentFilter() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BNCS_BROADCAST_CONNECTION);
        return intentFilter;
    }

    public boolean isConnected() {
        return this.mConnectionState == -2 || this.mConnectionState == -1 || this.mConnectionState == -3;
    }

    public void sendBroadcast(int i) {
        Intent intent = new Intent(BNCS_BROADCAST_CONNECTION);
        intent.putExtra("status", i);
        LocalBroadcastManager.getInstance(this.mContext).sendBroadcast(intent);
    }

    @Override
    public void disconnect() {
        super.disconnect();
        this.mCallback = null;
        LocalBroadcastManager.getInstance(this.mContext).unregisterReceiver(this.mReceiver);
    }

    public boolean isReady() {
        return this.mConnectionState == -3;
    }
}
